package cn.didi.feidi.androidorder.fragment.personal;


import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.api.ErrorEvent;
import cn.didi.feidi.androidorder.assembly.CommomDialog;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;
import cn.didi.feidi.androidorder.util.AppUtil;

public class PersonAdviceCallBackFragment extends FeidiBaseFragment {

    private static final String TAG = "PersonAdviceCallBack";

    private Boolean contentFinish = false;
    private Boolean mailFinish = false;

    private EditText adviceContentEdit;
    private TextView mailFalseTip;
    private EditText emailEdit;
    private TextView commitAdvice;

    private String adviceContent;
    private String email;

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonAdviceCallBackFragment newInstance(Bundle bundle) {
        PersonAdviceCallBackFragment fragment = new PersonAdviceCallBackFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_app_setting_advice_callback, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }


    @Override
    protected void initViews(View rootView) {
        adviceContentEdit = (EditText) rootView.findViewById(R.id.person_advice_callback_edit_advice_content);
        mailFalseTip = (TextView) rootView.findViewById(R.id.person_advice_callback_mail_fault_tip);
        emailEdit = (EditText) rootView.findViewById(R.id.person_advice_callback_mail_edit);
        commitAdvice = (TextView) rootView.findViewById(R.id.person_advice_callback_commit);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addListener() {
        adviceContentEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                adviceContent = s.toString().trim();
                if (!adviceContent.equals("")) {
                    contentFinish = true;
                    if (mailFinish) {
                        commitAdvice.setClickable(true);
                        commitAdvice.setBackgroundResource(R.drawable.bg_app_setting_advice_callback_commit_selector);
                    }
                } else {
                    contentFinish = false;
                    commitAdvice.setClickable(false);
                    commitAdvice.setBackgroundColor(Color.parseColor("#b0cbf5"));
                }
            }
        });

        emailEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                email = s.toString().trim();
                if (!AppUtil.checkmail(email)) {
                    mailFalseTip.setVisibility(View.VISIBLE);
                    mailFinish = false;
                    commitAdvice.setClickable(false);
                    commitAdvice.setBackgroundColor(Color.parseColor("#b0cbf5"));
                } else {
                    mailFalseTip.setVisibility(View.INVISIBLE);
                    mailFinish = true;
                    if (contentFinish) {
                        commitAdvice.setBackgroundResource(R.drawable.bg_app_setting_advice_callback_commit_selector);
                        commitAdvice.setClickable(true);
                    }
                }
            }
        });

        commitAdvice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (contentFinish && mailFinish) {
                    commitAdvice.setClickable(false);
                    Map<String, String> params = new HashMap<>();

                    params.put("uid", UserManager.getUserId());
                    params.put("token", UserManager.getToken());//故意的
                    params.put("content", adviceContent);
                    params.put("contact", email);
                    params.put("source", "0");//0用户

                    executeRequest(new AppActionRequest<Object>(Api.FEEDBACK, params,
                            new TypeToken<ApiResponse<Object>>() {
                            }.getType(), new ActionCallbackListener<Object>() {
                        @Override
                        public void onSuccess(String message, Object data) {
//                            makeToast("上传反馈建议");
                            showCommitFinishDialog();
                        }

                        @Override
                        public void onFailure(int errorEvent, String message) {
                            Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                            switch (errorEvent) {
                                case ErrorEvent.PARAM_NULL:
                                    makeToast("参数为空");
                                    break;
                                case ErrorEvent.PARAM_ILLEGAL:
                                    makeToast("参数为空");
                                    break;
                                case ErrorEvent.PARAM_ACCESS_FAILURE:
                                    makeToast("网络异常，访问失败");
                                    break;
                                default:
                                    makeToast("未知错误");
                                    break;
                            }
                        }
                    }));
                }
            }
        });
    }

    private void showCommitFinishDialog() {
        new CommomDialog(getContext(), R.style.dialog, "提交成功，感谢您的反馈", new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    commitAdvice.setClickable(true);
                    dialog.dismiss();
                    getActivity().finish();
                }
            }
        }, true, true).setPositiveButton("返回个人中心", "#347cf1").show();
    }

}
